using GameFramework;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityGameFramework.Editor.ResourceTools;
using UnityGameFramework.Runtime;
using UnityEditor;

namespace LS.Editor.Build
{
    public class BuildedResourcesMenu
    {

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/Windows", true)]
        public static bool HasBuildedResourcesForWindows()
        {
            return HasBuildedResources(Platform.Windows);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/Windows64", true)]
        public static bool HasBuildedResourcesForWindows64()
        {
            return HasBuildedResources(Platform.Windows64);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/MacOS", true)]
        public static bool HasBuildedResourcesForMacOS()
        {
            return HasBuildedResources(Platform.MacOS);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/Linux", true)]
        public static bool HasBuildedResourcesForLinux()
        {
            return HasBuildedResources(Platform.Linux);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/IOS", true)]
        public static bool HasBuildedResourcesForIOS()
        {
            return HasBuildedResources(Platform.IOS);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/Android", true)]
        public static bool HasBuildedResourcesForAndroid()
        {
            return HasBuildedResources(Platform.Android);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/WindowsStore", true)]
        public static bool HasBuildedResourcesForWindowsStore()
        {
            return HasBuildedResources(Platform.WindowsStore);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/WebGL", true)]
        public static bool HasBuildedResourcesForWebGL()
        {
            return HasBuildedResources(Platform.WebGL);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/Windows")]
        public static void CopyBuildedResourcesForWindows()
        {
            CopyBuildedResourcesToStreamingAsset(Platform.Windows);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/Windows64")]
        public static void CopyBuildedResourcesForWindows64()
        {
            CopyBuildedResourcesToStreamingAsset(Platform.Windows64);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/MacOS")]
        public static void CopyBuildedResourcesForMacOS()
        {
            CopyBuildedResourcesToStreamingAsset(Platform.MacOS);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/Linux")]
        public static void CopyBuildedResourcesForLinux()
        {
            CopyBuildedResourcesToStreamingAsset(Platform.Linux);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/IOS")]
        public static void CopyBuildedResourcesForIOS()
        {
            CopyBuildedResourcesToStreamingAsset(Platform.IOS);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/Android")]
        public static void CopyBuildedResourcesForAndroid()
        {
            CopyBuildedResourcesToStreamingAsset(Platform.Android);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/WindowsStore")]
        public static void CopyBuildedResourcesForWindowsStore()
        {
            CopyBuildedResourcesToStreamingAsset(Platform.WindowsStore);
        }

        [MenuItem("Lancer Studio/Copy Builded Resources To StreamingAsset/WebGL")]
        public static void CopyBuildedResourcesForWebGL()
        {
            CopyBuildedResourcesToStreamingAsset(Platform.WebGL);
        }

        public static bool HasBuildedResources(Platform platform)
        {
            var result = BuilderResultConfig.Instance.GetResult(platform);
            return result != null && !string.IsNullOrEmpty(result.outputPackagePath);
        }

        
        public static void CopyBuildedResourcesToStreamingAsset(Platform platform)
        {
            string folderPath = null;
            if (EditorPrefs.HasKey("BuildedResourcesPath"))
            {
                folderPath = EditorPrefs.GetString("BuildedResourcesPath");
            }
            else
            {
                folderPath = Application.dataPath;
            }
            folderPath = EditorUtility.OpenFolderPanel("Copy To",folderPath, string.Empty);
            
            if (string.IsNullOrEmpty(folderPath))
            {
                Debug.LogWarning("Copy Builded Resources Process is Canceled");
                return;
            }
            
            EditorPrefs.SetString("BuildedResourcesPath", folderPath);
            
            var result = BuilderResultConfig.Instance.GetResult(platform);
            if (result == null || string.IsNullOrEmpty(result.outputPackagePath))
            {
                throw new System.Exception($"You should build packages for platform [{platform.ToString()}] first!");
            }

            if (!Directory.Exists(result.outputPackagePath))
            {
                throw new System.Exception($"Packages Path for platform [{platform.ToString()}] is not existed! path: {result.outputPackagePath}");
            }

            string outputPackagePath = result.outputPackagePath;
            string directory = folderPath;
            Debug.Log($"Copy resources to StreamingAsset path: {folderPath}. platform: {platform.ToString()}, from {outputPackagePath}");
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        
            string[] fileNames = Directory.GetFiles(outputPackagePath, "*", SearchOption.AllDirectories);

            foreach (string fileName in fileNames)
            {
                string destFileName = Utility.Path.GetRegularPath(Path.Combine(directory, fileName.Substring(outputPackagePath.Length)));
                
                FileInfo destFileInfo = new FileInfo(destFileName);
                if (!destFileInfo.Directory.Exists)
                    destFileInfo.Directory.Create();

                File.Copy(fileName, destFileName, true);
            }

            AssetDatabase.Refresh();
        }
    }
}
